package gui.assiste;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import framework.AnnuaireCocola;
import framework.classification.library.AbstractClassfication;
import framework.classification.library.SmartClassfication;
import gui.utils.DegradedPanel;
import gui.utils.JCoolButton;
import gui.utils.JCoolToggleButton;




public class CreateClAssisted extends JDialog{

	public static String NEXT = "next";
	public static String PREVIOUS = "previous";
	public static String CLOSE = "close";
	
	
	public static String descComplete = "";
	public static String descSmart = "(recommended)\n" +
			"											";
	public static String descFast = "";
	
	public static String descOrdered = "(recommended)\n" +
			"							";
	public static String descNoOrdered = "";
	
	public static String descNiv1 = "Level of partitioning classification";
	public static String descNiv2 = "Level of partitioning classification";
	
	public static String descLattice = "Use galois hierarchy for classification\n" +
			"							";
	public static String descGSH = "(recommended)\n" +
			"						Use galois sub-hierarchy for classification\n" +
			"						The classification is more fast and more simplifies";

	public class PanelStep extends JPanel implements ActionListener{



		private CreateClAssisted ass;

		private String name;

		private String next;

		private JTextArea area;

		private ArrayList<String> description;

		private String nextState;

		private String previousState;

		private ArrayList<JCoolToggleButton> buttons;

		private ArrayList<Object> results;

		private Object finalResult = null;

		public PanelStep(String name,String next, CreateClAssisted assis){
			super();
			this.name = name; 
			this.next = next;
			this.ass = assis;
			description = new ArrayList<String>();
			buttons = new ArrayList<JCoolToggleButton>();
			results = new ArrayList<Object>();


		}
		public void createComponent(){
			setLayout(new BorderLayout());

			Box mainBox = Box.createVerticalBox();
			mainBox.add(Box.createVerticalStrut(25));
			mainBox.add(createTitle(name));
			mainBox.add(Box.createVerticalStrut(40));
			mainBox.add(createGroupeButton());
			mainBox.add(Box.createVerticalStrut(45));
			mainBox.add(createArea());

			add(mainBox,BorderLayout.CENTER);
			add(createBottom(),BorderLayout.SOUTH);

			buttons.get(0).doClick();
		}

	
		private JComponent createGroupeButton(){
			Box hbox = Box.createHorizontalBox();
			ButtonGroup group = new ButtonGroup();
			for (JCoolToggleButton b : buttons){
				group.add(b);
				hbox.add(b);
			}
			return hbox;
		}

		private JComponent createTitle(String title){
			JPanel panelLabel = new JPanel(new FlowLayout());
			JLabel label = new JLabel(title);
			label.setForeground(Color.WHITE);
			panelLabel.add(label);
			panelLabel.setBackground(Color.BLACK);
			panelLabel.setPreferredSize(new Dimension(250,25));
			panelLabel.setMaximumSize(new Dimension(250,25));
			return panelLabel;
		}


		private JTextArea createArea(){

			area = new JTextArea();
			area.setBorder(BorderFactory.createTitledBorder("Description"));
			area.setBackground(new Color(175,175,175));
			
			return area;
		}

		private JButton createButton(String name,String action){
			JButton b = new JCoolButton(name,new Color(75,75,75), new Color(0,101,135));
			b.setActionCommand(action);
			b.addActionListener(this);
			return b;
		}

		public void addOption(String name,final Object result,final String description,final String next,final String prev){

			JCoolToggleButton button = new JCoolToggleButton(name);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					area.setText(description);
					finalResult = result; 
					nextState = next;
					previousState = prev;

				}
			});
			buttons.add(button);
		}


		private JComponent createBottom(){
			Box mainbox = Box.createHorizontalBox();
			mainbox.add(Box.createHorizontalGlue());
			mainbox.add(createButton("previous",PREVIOUS));
			mainbox.add(createButton(next,NEXT));
			mainbox.add(Box.createHorizontalStrut(20));
			mainbox.add(createButton("cancel",CLOSE));
			return mainbox;

		}
		
		public void actionPerformed(ActionEvent e) {
			String action = e.getActionCommand();
			if (action.equals(NEXT)){


				ass.showStep(nextState);

			}
			if (action.equals(PREVIOUS)){
				ass.showStep(previousState);
				//finalResult = null;
			}
			if (action.equals(CLOSE)){
				ass.dispose();
			}

		}

		public String getName(){
			return name;
		}
		public Object getResult(){return finalResult;}

	}











	private CardLayout stepLayout;
	private JPanel stepPanel;
	private ArrayList<PanelStep> list;
	private ArrayList<Object> listResult;


	public CreateClAssisted() { 
		setTitle("Votre choix");
		setModalityType(ModalityType.APPLICATION_MODAL);
		list = new ArrayList<PanelStep>();
		stepLayout = new CardLayout();
		listResult = null;
		stepPanel = new JPanel();
		stepPanel.setLayout(stepLayout);

		PanelStep step1 = new PanelStep("Classification","next",this);
		PanelStep step2 = new PanelStep("PartitionLevel","next",this);
		PanelStep step3 = new PanelStep("Substitumode","next",this);
		PanelStep step4 = new PanelStep("LatticeGeneration","create",this);

		
		step1.addOption("complete",AnnuaireCocola.COMPLETE,descComplete,step4.getName(),step1.getName());
		step1.addOption("fast",AnnuaireCocola.FAST,descFast,step3.getName(),step1.getName());
		step1.addOption("smart",AnnuaireCocola.SMART,descSmart,step2.getName(),step1.getName());
		step1.createComponent();

		step2.addOption("Niv 1",SmartClassfication.NIV1,descNiv1,step4.getName(),step1.getName());
		step2.addOption("Niv 2",SmartClassfication.NIV2,descNiv2,step4.getName(),step1.getName());
		step2.createComponent();

		step3.addOption("ordered",true,descOrdered,step4.getName(),step1.getName());
		step3.addOption("no ordered",false,descNoOrdered,step4.getName(),step1.getName());
		step3.createComponent();

		step4.addOption("lattice",AnnuaireCocola.LATTICE,descLattice,"Finish",step1.getName());
		step4.addOption("gsh",AnnuaireCocola.GSH,descGSH,"Finish",step1.getName());
		step4.createComponent();


		stepPanel.add(step1.getName(), step1);
		stepPanel.add(step2.getName(), step2);
		stepPanel.add(step3.getName(), step3);
		stepPanel.add(step4.getName(), step4);

		list.add(step1);list.add(step2);list.add(step3);list.add(step4);


		this.setMinimumSize(new Dimension(400,300));
		this.setMaximumSize(new Dimension(400,300));
		this.setPreferredSize(new Dimension(400,300));
		this.setResizable(false);
		this.setContentPane(stepPanel);
	}


	public void showStep(String step){
		if (step.equals("Finish")){
			finish();
		}
		else{
			stepLayout.show(stepPanel,step);
		}
	}

	public void finish(){
		listResult = new ArrayList<Object>();
		for (PanelStep ps : list){
			if (ps.getResult() != null){
				listResult.add(ps.getResult());
			}
		}
		dispose();
	}

	public ArrayList<Object> getListResults(){return listResult;}


	public static ArrayList<Object> openForm() {
		JFrame parent = new JFrame();
		CreateClAssisted form = new CreateClAssisted();
		form.setLocationRelativeTo(parent);
		form.setVisible(true);

		if (form.getListResults() != null){
			return form.getListResults();
		}
		else return null;

		
	}





}
